import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe
import os

def Smile_Net(cf,md,mw):
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    caffe_root = cf
    sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    model_def=md
    model_weights=mw
    mean_data=np.array([129.1863,104.7624,93.5940])
    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))
    transformer.set_mean('data',mean_data)
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,128,128)
    return (transformer,net)

def Is_Smile(tf,nt,img_path):
    transformer=tf
    net=nt
    image = caffe.io.load_image(img_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    output = net.forward()
    prob = output['smile_prob'][0]
    return prob.argmax()#1 to smile

# caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
# model_def='F:/face-recognition/DeepFace-master/FaceSmile/deploy_smile.prototxt'
# model_weights='F:/face-recognition/DeepFace-master/FaceSmile/face_smile.model'
# img_path='F:/IMS/test_photos/face/TA5.jpg'
# tf,nt=Smile_Net(caffe_root,model_def,model_weights)
# r=Is_Smile(tf,nt,img_path)
# labels = ['smooth','smile']
# print labels[r]